package com.terrier.ui.controls
{
    import com.terrier.ui.core.WxComponent;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxTabBarSkin;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.events.Event;
    import flash.geom.Point;
    import flash.utils.Dictionary;

    /**
     * 当被选中的选项卡改变时调度
     * @eventType flash.events.Event.CHANGE
     */
    [Event(name = "change", type = "flash.events.Event")]

    /**
     * 选项卡组件
     * @author Jarvis.weng
     */
    public class WxTabBar extends WxComponent
    {
        private var _tabBtnList:Array = [];

        private var _tabDataDict:Dictionary = new Dictionary();

        private var _group:WxCheckButtonGroup;

        private var _direction:String = WxTabBarDirection.HORIZONTAL;

        private var _autoSize:Boolean;

        /**
         * 构造函数
         */
        public function WxTabBar()
        {
            super();

            _group = new WxCheckButtonGroup();
            _group.addEventListener(Event.CHANGE, tabChangeHandler);
        }

        /**
         * 获取或设置一个布尔值, 指示是否自动调整Tab的大小.
         * @default false
         * @return
         */
        public function get autoSize():Boolean
        {
            return _autoSize;
        }

        /**
         * @private
         */
        public function set autoSize(value:Boolean):void
        {
            _autoSize = value;

            invalidateLayout();
        }

        /**
         * TabBar的排列方向. 有水平排列和垂直排列两种形式.
         * 常量定义为 <code>WxTabBarDirection.VERTICAL</code> 和  <code>WxTabBarDirection.HORIZONTAL</code>
         * @see com.monkey.ui.controls.WxTabBarDirection
         * @return
         */
        public function get direction():String
        {
            return _direction;
        }

        /**
         * @private
         */
        public function set direction(value:String):void
        {
            if (_direction == value)
            {
                return;
            }

            _direction = value;
            invalidateLayout();
        }

        /**
         * 当前选中的选项卡所对应的数据
         * @return
         */
        public function get currentData():*
        {
            return _group.currentData;
        }

        /**
         * 当前选中的选项卡的文本标签
         * @return
         */
        public function get currentLabel():String
        {
            return _group.currentButton.label;
        }

        /**
         * 添加选项卡.
         * @param label 选项卡的文本
         * @param data 对应的数据
         */
        public function addTab(label:String, data:*):void
        {
            addTabAt(label, data, _tabBtnList.length);
        }

        /**
         * 在指定的位置添加选项卡
         * @param label 选项卡的文本
         * @param data 对应的数据
         * @param index 位置的索引
         */
        public function addTabAt(label:String, data:*, index:int):void
        {
            if (index < 0 || _tabBtnList.length < index)
            {
                return;
            }
            var tab:WxCheckButton = new WxCheckButton();
            tab.label = label;

            if (index == _tabBtnList.length)
            {
                _tabBtnList.push(tab);
            }
            else
            {
                _tabBtnList.splice(index, 0, tab);
            }
            _tabDataDict[tab] = data;

            _group.addButtonAndData(tab, data);

            addChild(tab);

            invalidateLayout();
        }

        /**
         * 删除对应文本的选项卡
         * @param label 选项卡的文本
         */
        public function removeTab(label:String):void
        {
            var index:int = -1;
            for (var i:int = 0; i < _tabBtnList.length; ++i)
            {
                var tab:WxCheckButton = _tabBtnList[i];
                if (tab.label == label)
                {
                    index = i;
                    break;
                }
            }

            removeChildAt(index);
        }

        /**
         * 删除指定位置的选项卡
         * @param index
         */
        public function removeTabAt(index:int):void
        {
            if (index < 0 || _tabBtnList.length <= index)
            {
                return;
            }

            var tab:WxCheckButton = _tabBtnList.splice(index, 1)[0];
            delete _tabDataDict[tab];

            _group.removeButton(tab);

            removeChild(tab);

            invalidateLayout();
        }

        /**
         * 选中指定位置的选项卡
         * @param index
         */
        public function selectTabAt(index:int):void
        {
            if (index < 0 || _tabBtnList.length <= index)
            {
                return;
            }

            _group.selectButtonAt(index);
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getTabBarSkin(skinID));
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

            invalidateLayout();
        }

        /**
         * @private
         */
        override public function invalidateLayout():void
        {
            if (skin != null)
            {
                var tabNum:int = _tabBtnList.length
                skin.setTabInfo(tabNum, direction);
                for (var i:int = 0; i < tabNum; ++i)
                {
                    var tab:WxCheckButton = _tabBtnList[i];
                    tab.setSkin(skin.getTabSkin(i));
					tab.autoSize = autoSize;
                }
            }
            super.invalidateLayout();
        }

        /**
         * @private
         */
        override protected function updateLayout():void
        {
            super.updateLayout();

            if (skin == null)
            {
                return
            }

            if (!skin.customLayout)
            {
                adjustTabSize();
				calculateTabSize();
				
                var pos:Point = new Point();
                for (var i:int = 0; i < _tabBtnList.length; ++i)
                {
                    var tab:WxCheckButton = _tabBtnList[i];
                    tab.x = pos.x;
                    tab.y = pos.y;
                    if (_direction == WxTabBarDirection.HORIZONTAL)
                    {
                        pos.x += tab.width;
                    }
                    else
                    {
                        pos.y += tab.height;
                    }
                }
            }
        }

        private function adjustTabSize():void
        {
            if (_autoSize || _tabBtnList.length == 0)
            {
                return;
            }
            var tabWidth:Number = _width / _tabBtnList.length;
            var tabHeight:Number = _height / _tabBtnList.length;
            for (var i:int = 0; i < _tabBtnList.length; ++i)
            {
                var tab:WxLabelButton = _tabBtnList[i];
                if (_direction == WxTabBarDirection.HORIZONTAL)
                {
                    tab.width = tabWidth;
                    tab.height = _height;
                }
                else
                {
                    tab.height = tabHeight;
                    tab.width = _width;
                }
            }
        }
		
		private function calculateTabSize():void
		{
			if (!_autoSize)
			{
				return;
			}
			_width = _height = 0;
			for (var i:int = 0; i < _tabBtnList.length; ++i)
			{
				var tab:WxLabelButton = _tabBtnList[i];
				if (_direction == WxTabBarDirection.HORIZONTAL)
				{
					_width += tab.width;
					_height = Math.max(_height, tab.height);
				}
				else
				{
					_width = Math.max(_width, tab.width);
					_height += tab.height;
				}
			}
		}

        private function tabChangeHandler(e:Event):void
        {
            dispatchEvent(e);
        }

        private function get skin():IWxTabBarSkin
        {
            return _skin as IWxTabBarSkin;
        }
    }
}
